/*
 * $Id: frmNarrationOnly.java 22 2010-03-24 14:16:47Z eldon_r $
 *
 * Created on 06/12/2009, 11:35:45 PM
 */

package customqif;

import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Edgeberg <eldon_r@users.sf.net>
 */
public class frmNarrationOnly extends javax.swing.JFrame {

    /** Creates new form frmNarrationOnly */
    public frmNarrationOnly(frmCustomiseQIF objParent, String strNarrationOnlyPatterns) {
        initComponents();
        strNOP = strNarrationOnlyPatterns;
        parent = objParent;
        loadGrid(strNOP);
    }

    String strNOP = "";
    frmCustomiseQIF parent;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblNarrationOnlyPatterns = new javax.swing.JTable();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnDel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);

        tblNarrationOnlyPatterns.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null}
            },
            new String [] {
                "Narration Only Patterns"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblNarrationOnlyPatterns.setColumnSelectionAllowed(true);
        tblNarrationOnlyPatterns.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblNarrationOnlyPatterns);
        tblNarrationOnlyPatterns.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblNarrationOnlyPatterns.getColumnModel().getColumn(0).setResizable(false);

        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnAdd.setText("+");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnDel.setText("-");
        btnDel.setToolTipText("Delete the selected row");
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnOK)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnDel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAdd)
                        .addGap(18, 18, 18)
                        .addComponent(btnDel))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnOK))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        DefaultTableModel tableModel = (DefaultTableModel) tblNarrationOnlyPatterns.getModel();
        tableModel.addRow(new Object[]{""});
        tblNarrationOnlyPatterns.setModel(tableModel);
    }//GEN-LAST:event_btnAddActionPerformed

    public void handleException(Component window, String errorText, String dialogTitle) {
        //if (getOption("HitAuthor", false).equals("Yes")) {
        //    System.err.println("Author email not implemented...");
        //}
        //if (getOption("ErrorsToStdout", false).equalsIgnoreCase("Yes")) {
        System.out.print(dialogTitle + ": " + errorText);
        //}
        JOptionPane.showMessageDialog(window, errorText, dialogTitle, JOptionPane.ERROR_MESSAGE);
    }

    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
        if (tblNarrationOnlyPatterns.getSelectedRow() >= 0) {
            DefaultTableModel tableModel = (DefaultTableModel) tblNarrationOnlyPatterns.getModel();
            tableModel.removeRow(tblNarrationOnlyPatterns.getSelectedRow());
            tblNarrationOnlyPatterns.setModel(tableModel);
        } else {
            handleException(this, "Select a row to delete, then try again.", "Invalid Delete operation attempted.");
        }
    }//GEN-LAST:event_btnDelActionPerformed

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        strNOP = "";
        DefaultTableModel tableModel = (DefaultTableModel) tblNarrationOnlyPatterns.getModel();
        for ( int i=0 ; i<tableModel.getRowCount() ; i++) {
            strNOP = strNOP + tableModel.getValueAt(i, 0) + "\t";
        }
        strNOP = strNOP.trim();
        parent.strNarrationPatterns = strNOP;
        parent.tabStrToArray();
        this.dispose();
    }//GEN-LAST:event_btnOKActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDel;
    private javax.swing.JButton btnOK;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblNarrationOnlyPatterns;
    // End of variables declaration//GEN-END:variables

    private void loadGrid(String strNarrationOnlyPatterns) {
        int intTabs = 0;
        String aryNOPElements[];
        DefaultTableModel tableModel = (DefaultTableModel) tblNarrationOnlyPatterns.getModel();
        tableModel.removeRow(0);
        if (!strNarrationOnlyPatterns.trim().equals("")) {
            aryNOPElements = strNarrationOnlyPatterns.split("\t");
            intTabs = aryNOPElements.length;
            for (int i=0 ; i<intTabs ; i++) {
                tableModel.addRow(new Object[]{aryNOPElements[i]});
            }
        }
        tblNarrationOnlyPatterns.setModel(tableModel);
    }

}
